# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=lapack
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-blas"
         "${MINGW_PACKAGE_PREFIX}-cblas"
         "${MINGW_PACKAGE_PREFIX}-lapacke"
         "${MINGW_PACKAGE_PREFIX}-${_realname}64"
         "${MINGW_PACKAGE_PREFIX}-blas64"
         "${MINGW_PACKAGE_PREFIX}-cblas64"
         "${MINGW_PACKAGE_PREFIX}-lapacke64")
pkgver=3.12.1
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="Linear Algebra PACKage (mingw-w64)"
url="https://www.netlib.org/lapack"
msys2_repository_url="https://github.com/Reference-LAPACK/lapack"
msys2_references=(
  'archlinux: lapack'
  "cpe: cpe:/a:lapack_project:lapack"
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://github.com/Reference-LAPACK/lapack/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-sized-init-with-integer-8.patch::https://patch-diff.githubusercontent.com/raw/Reference-LAPACK/lapack/pull/1094.patch
        0002-type-mismatch-with-integer-8.patch::https://github.com/Reference-LAPACK/lapack/commit/0799b59571a4bbb434c62ef2346146123aa19d8d.patch
        0003-line-reflow-with-integer-8.patch::https://github.com/Reference-LAPACK/lapack/pull/1099.patch)
sha256sums=('2ca6407a001a474d4d4d35f3a61550156050c48016d949f0da0529c0aa052422'
            '658a558b012101089322e3af8246568e635776eebcdb588803af7f245f676513'
            '1cd1328111c562c635bcc5d69b1088f763825674445c2c011f02fd4846665dcd'
            'dce6370e0f4d1d8c4cbac4c8c50e87e3f6f7ab3d62903760b6abc1aa8158e221')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-sized-init-with-integer-8.patch \
    0002-type-mismatch-with-integer-8.patch \
    0003-line-reflow-with-integer-8.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "y"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  fi

  for _shared in ON OFF; do
    for _index_64 in ON OFF; do
      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      ${MINGW_PREFIX}/bin/cmake.exe \
        -G"Ninja" \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        "${_extra_config[@]}" \
        -DBUILD_SHARED_LIBS=${_shared} \
        -DBUILD_INDEX64=${_index_64} \
        -DCMAKE_NEED_RESPONSE=ON \
        -DBUILD_TESTING=OFF \
        -DLAPACKE_WITH_TMG=ON \
        -DCBLAS=ON \
        -DBUILD_DEPRECATED=ON \
        -S ${_realname}-${pkgver} \
        -B build-${MSYSTEM}-sh-${_shared}-64-${_index_64}

      ${MINGW_PREFIX}/bin/cmake.exe --build build-${MSYSTEM}-sh-${_shared}-64-${_index_64}
    done
  done
}

check() {
  # Many of the tests for CBLAS are failing
  for _shared in ON OFF; do
    for _index_64 in ON OFF; do
      ${MINGW_PREFIX}/bin/cmake.exe \
        -DBUILD_TESTING=ON \
        -S ${_realname}-${pkgver} \
        -B build-${MSYSTEM}-sh-${_shared}-64-${_index_64}
      ${MINGW_PREFIX}/bin/cmake.exe --build build-${MSYSTEM}-sh-${_shared}-64-${_index_64}
      ${MINGW_PREFIX}/bin/ctest ctest --test-dir build-${MSYSTEM}-sh-${_shared}-64-${_index_64} || true
    done
  done
}

package_lapack() {
  depends=("${MINGW_PACKAGE_PREFIX}-blas"
           $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))

  for _shared in OFF ON; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-sh-${_shared}-64-OFF
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  rm -r "$pkgdir"${MINGW_PREFIX}/bin/{libblas.*,libcblas.*,liblapacke.*}
  rm -r "$pkgdir"${MINGW_PREFIX}/lib/{libblas.*,libcblas.*,liblapacke.*}
  rm -r "$pkgdir"${MINGW_PREFIX}/lib/pkgconfig/{blas.*,cblas.*,lapacke.*}
  rm -r "$pkgdir"${MINGW_PREFIX}/lib/cmake/{cblas*,lapacke*}
  rm -r "$pkgdir"${MINGW_PREFIX}/include
}

package_blas() {
  pkgdesc='Basic Linear Algebra Subprograms (mingw-w64)'
  depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))

  for _shared in OFF ON; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-sh-${_shared}-64-OFF/BLAS
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" -t "$pkgdir"${MINGW_PREFIX}/share/licenses/blas/
}

package_cblas() {
  pkgdesc='C interface to BLAS (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-blas")

  for _shared in OFF ON; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-sh-${_shared}-64-OFF/CBLAS
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" -t "$pkgdir"${MINGW_PREFIX}/share/licenses/cblas/
}

package_lapacke() {
  pkgdesc='C interface to LAPACK (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-lapack")

  for _shared in OFF ON; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-sh-${_shared}-64-OFF/LAPACKE
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" -t "$pkgdir"${MINGW_PREFIX}/share/licenses/lapacke/
}

package_lapack64() {
  pkgdesc="Linear Algebra PACKage (64-bit integers) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-blas64"
           $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))

  for _shared in OFF ON; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-sh-${_shared}-64-ON
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}64/LICENSE"

  rm -r "$pkgdir"${MINGW_PREFIX}/bin/{libblas64.*,libcblas64.*,liblapacke64.*}
  rm -r "$pkgdir"${MINGW_PREFIX}/lib/{libblas64.*,libcblas64.*,liblapacke64.*}
  rm -r "$pkgdir"${MINGW_PREFIX}/lib/pkgconfig/{blas64.*,cblas64.*,lapacke64.*}
  rm -r "$pkgdir"${MINGW_PREFIX}/lib/cmake/{cblas64*,lapacke64*}
  rm -r "$pkgdir"${MINGW_PREFIX}/include
}

package_blas64() {
  pkgdesc='Basic Linear Algebra Subprograms (64-bit integers) (mingw-w64)'
  depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))

  for _shared in OFF ON; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-sh-${_shared}-64-ON/BLAS
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" -t "$pkgdir"${MINGW_PREFIX}/share/licenses/blas64/
}

package_cblas64() {
  pkgdesc='C interface to BLAS (64-bit integers) (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-blas64")
  optdepends=("${MINGW_PACKAGE_PREFIX}-blas: development headers")

  for _shared in OFF ON; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-sh-${_shared}-64-ON/CBLAS
  done

  rm -r "$pkgdir"${MINGW_PREFIX}/include # Provided by cblas

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" -t "$pkgdir"${MINGW_PREFIX}/share/licenses/cblas64/
}

package_lapacke64() {
  pkgdesc='C interface to LAPACK (64-bit integers) (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-lapack64")
  optdepends=("${MINGW_PACKAGE_PREFIX}-lapacke: development headers")

  for _shared in OFF ON; do
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-sh-${_shared}-64-ON/LAPACKE
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" -t "$pkgdir"${MINGW_PREFIX}/share/licenses/lapacke64/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
